<template>
  <el-card>
    <!-- 展示表格 -->
    <el-table
      :data="skuList"
      border
      v-loading="isLoading"
      class="sku-list-table"
    >
      <el-table-column
        type="index"
        label="序号"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片" align="center">
        <template #default="{ row }">
          <img
            :src="row.skuDefaultImg || './imgs/playlist_300.png'"
            :alt="row.skuName"
            class="sku-img"
          />
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight"></el-table-column>
      <el-table-column label="价格（元）" prop="price"></el-table-column>
      <el-table-column label="操作" width="250px" align="center">
        <template #default="{ row }">
          <el-button
            size="mini"
            :type="row.isSale ? 'success' : 'info'"
            :icon="row.isSale ? 'el-icon-bottom' : 'el-icon-top'"
            :title="row.isSale ? '点击下架' : '点击上架'"
            @click="saleOrNot(row)"
          ></el-button>
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            title="编辑SKU"
            @click="toEditSKu(row)"
          ></el-button>
          <el-button
            size="mini"
            type="info"
            icon="el-icon-info"
            title="查看SKU信息"
            @click="showSkuInfo(row)"
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            title="删除"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="100"
      layout=" prev, pager, next, jumper, sizes, total"
      :total="pagination.total"
    />
    <!-- 抽屉 -->
    <el-drawer :visible.sync="drawer" size="50%" :with-header="false">
      <div class="table-wrap">
        <el-descriptions
          :column="1"
          border
          size="large"
          :labelStyle="{
            textAlign: 'right',
            fontWeight: '700',
            width: '150px'
          }"
          v-loading="drawerLoading"
        >
          <el-descriptions-item label="名称" label-align="right">
            {{ singleSku.skuName }}
          </el-descriptions-item>
          <el-descriptions-item label="描述" label-align="right">
            {{ singleSku.skuDesc }}
          </el-descriptions-item>
          <el-descriptions-item label="价格" label-align="right">
            {{ singleSku.price }}
          </el-descriptions-item>
          <el-descriptions-item label="重量" label-align="right">
            {{ singleSku.weight }}
          </el-descriptions-item>
          <el-descriptions-item label="平台属性" label-align="right">
            <el-tag
              class="sku-tag"
              size="mini"
              type="primary"
              v-for="attr in singleSku.skuAttrValueList"
              :key="attr.id"
            >
              {{ `${attr.attrName} - ${attr.valueName}` }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="销售属性" label-align="right">
            <el-tag
              class="sku-tag"
              size="mini"
              type="primary"
              v-for="attr in singleSku.skuSaleAttrValueList"
              :key="attr.id"
            >
              {{ `${attr.saleAttrName} - ${attr.saleAttrValueName}` }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>

        <div class="swiper-wrap" v-loading="drawerLoading">
          <el-carousel trigger="click" :autoplay="false">
            <el-carousel-item
              v-for="img in singleSku.skuImageList"
              :key="img.id"
            >
              <img :src="img.imgUrl" :alt="img.imgName" class="swiper-img" />
            </el-carousel-item>
          </el-carousel>
        </div>
      </div>
    </el-drawer>
  </el-card>
</template>

<script>
import {
  getSkuListApi,
  getSkuInfoApi,
  skuOnSaleApi,
  skuCancelSaleApi
} from '@/api/product/skuApi'
export default {
  name: 'SkuList',
  data() {
    return {
      isLoading: false,
      drawer: false,
      drawerLoading: false,
      skuList: [],
      pagination: {
        page: 1,
        total: 0,
        limit: 5
      },
      singleSku: {
        skuName: '',
        price: 0,
        skuDesc: '',
        weight: '',
        skuAttrValueList: [],
        skuImageList: [],
        skuSaleAttrValueList: []
      }
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    // 去编辑页面
    async toEditSKu(row) {
      this.isLoading = true
      const res = await getSkuInfoApi(row.id)
      this.$emit('setSkuInfo', res.data)
      this.isLoading = false
    },
    // 是否上加
    async saleOrNot(row) {
      if (row.isSale) {
        await skuCancelSaleApi(row.id)
      } else {
        await skuOnSaleApi(row.id)
      }
      this.getSkuList()
      this.$message.success('操作成功')
    },
    // 打开抽屉
    showSkuInfo(row) {
      this.drawer = true
      this.getSkuinfo(row.id)
    },
    // 获取单个sku信息的方法
    async getSkuinfo(id) {
      this.drawerLoading = true
      const res = await getSkuInfoApi(id)
      this.singleSku.skuName = res.data.skuName
      this.singleSku.price = res.data.price
      this.singleSku.skuDesc = res.data.skuDesc
      this.singleSku.weight = res.data.weight
      this.singleSku.skuAttrValueList = res.data.skuAttrValueList
      this.singleSku.skuImageList = res.data.skuImageList
      this.singleSku.skuSaleAttrValueList = res.data.skuSaleAttrValueList
      this.drawerLoading = false
      console.log(res)
    },
    // 封装发送请求的方法
    async getSkuList() {
      this.isLoading = true
      const res = await getSkuListApi(
        this.pagination.page,
        this.pagination.limit
      )
      this.pagination.total = res.data.total
      this.skuList = res.data.records
      this.isLoading = false
    },
    // 改变pageSize
    handleSizeChange(size) {
      this.pagination.limit = size
      this.getSkuList()
    },
    // 切换页码
    handleCurrentChange(page) {
      this.pagination.page = page
      this.getSkuList()
    }
  }
}
</script>

<style scoped>
.sku-img {
  height: 80px;
}
.sku-list-table {
  margin-bottom: 20px;
}
.table-wrap {
  padding: 50px;
}
.swiper-wrap {
  padding: 80px 200px;
}
.swiper-img {
  width: 100%;
  height: 100%;
}
.sku-tag {
  margin: 0 5px;
}
</style>
<style>
.el-carousel__item {
  text-align: center;
}
.el-carousel__container {
  background-color: #f3f3f3;
}
.el-carousel__item {
  padding: 20px;
}
.el-carousel__indicator--horizontal.is-active button {
  background-color: #ccc;
}
</style>
